package com.lut.edu.apm.commodityCenter.conteroller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lut.edu.apm.commodityCenter.pojo.CommodityOrder;
import com.lut.edu.apm.commodityCenter.service.CommodityOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品管理
 *
 * @author Zhao Suzhen
 * @date `2024/4/20`
 */
@Controller
@RequestMapping("/product")
public class ProductController {


    private final Logger logger = LoggerFactory.getLogger(CommodityController.class);

    @Autowired
    CommodityOrderService commodityOrderService;

    /**
     * 分页查询商品
     *
     * @param startPaging 起始页(从1开始)
     * @return
     */
    @GetMapping("queryPaging")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin:manag')")
    public String queryProductPaging(@RequestParam("startPaging") int startPaging,
                                     @RequestParam("queryProductName") String queryProductName,
                                     @RequestParam("queryBrand") String queryBrand,
                                     @RequestParam("queryCompany") String queryCompany,
                                     @RequestParam("queryProductItem") String queryProductItem,
                                     @RequestParam("queryReserve") List<Integer> queryReserve) {
        Page<CommodityOrder> records = commodityOrderService.queryProductPaging(startPaging, queryProductName,
                queryBrand, queryCompany, queryProductItem, queryReserve);
        JSONArray resultJson = JSONArray.parseArray(JSONObject.toJSONString(records.getRecords()));
        JSONObject result = new JSONObject();
        result.put("data", resultJson);
        result.put("PagingTotal", records.getTotal());
        return result.toJSONString();
    }

    /**
     * 模糊查询商品
     * @param queryName
     * @param queryValue
     * @return
     */
    @GetMapping("queryFuzzy")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin:manag')")
    public String queryFuzzy(@RequestParam("queryName") String queryName, @RequestParam("queryValue") String queryValue) {
        List<Map<String, Object>> result = new ArrayList<>();
        if (!queryValue.isEmpty() && !queryName.isEmpty()) {
            result = commodityOrderService.queryFuzzyName(queryName, queryValue);
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * 查询最大库存数量，用户库存管理模块的滑块
     * @return
     */
    @GetMapping("queryMaxReserve")
    @PreAuthorize("hasAuthority('admin:manag')")
    @ResponseBody
    public String queryMaxReserve() {
        JSONObject result = new JSONObject();
        result.put("count", commodityOrderService.queryMaxReserve());
        return result.toJSONString();
    }

}
